package com.woniuxy.goods.service.impl;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.goods.model.vo.UpdateGoodsVo;
import com.woniuxy.model.entity.Goods;
import com.woniuxy.goods.mapper.GoodsMapper;
import com.woniuxy.goods.service.GoodsService;
import com.woniuxy.model.vo.AddGoodsVo;
import com.woniuxy.model.vo.GoodsVo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper,Goods> implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;


    @Override
    public Page<Goods> selectPage(GoodsVo goodsVo) {
        Page<Goods> page = new Page<>(goodsVo.getCurrentPage(), goodsVo.getSize(),true);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        // where status=’0’
        wrapper.eq("status", "0");

        //page 参数1：页码   参数2：条数
        Page<Goods> goodsList = goodsMapper.selectPage(page,wrapper);

        return goodsList;

    }
    @Override
    public Page<Goods> selectPage01(GoodsVo goodsVo) {
        Page<Goods> page = new Page<>(goodsVo.getCurrentPage(), goodsVo.getSize(),true);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        // where status=’0’
        wrapper.ne("status", "1");

        //page 参数1：页码   参数2：条数
        Page<Goods> goodsList = goodsMapper.selectPage(page,wrapper);

        return goodsList;

    }

    @Override
    public Page<Goods> selectPage03(GoodsVo goodsVo) {
        Page<Goods> page = new Page<>(goodsVo.getCurrentPage(), goodsVo.getSize(),true);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        // where status=’0’
        wrapper.eq("status", "3");

        //page 参数1：页码   参数2：条数
        Page<Goods> goodsList = goodsMapper.selectPage(page,wrapper);

        return goodsList;

    }

    @Override
    public Page<Goods> selectPageCondition(GoodsVo goodsVo){
        goodsVo.setStatus("0");
        // 此处若使用的对象在数据库中没有真实存在的话(即没用相应的mapper.xml)，那么需要手动将缓存存入到 lambda 中去 (lambda 有提供辅助类 TableInfoHelper)
        // 其次 如果使用的是数据库中的表对象，如果字段有 @TableField(exist = false) 注解
        // 那么不能以这个字段作为lambda 的条件，因为有这个注解那么这个列就不会被加载到 lambda 的缓存当总去
        // 添加下面这行就能解决这2个问题
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), GoodsVo.class);

//        //将date转为String
//        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        goodsVo.setStatus("0");
        //当前页，每页大小
        Page<Goods> page = new Page<>(goodsVo.getCurrentPage(), goodsVo.getSize());
        LambdaQueryWrapper<GoodsVo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(goodsVo.getGoodsName()),GoodsVo::getGoodsName,goodsVo.getGoodsName())
                .eq(StringUtils.isNotBlank(goodsVo.getGoodsNumber()),GoodsVo::getGoodsNumber,goodsVo.getGoodsNumber())//商品编号
                .eq(StringUtils.isNotBlank(goodsVo.getArea()),GoodsVo::getArea,goodsVo.getArea())//区域
                .eq(StringUtils.isNotBlank(goodsVo.getProducter()),GoodsVo::getProducter,goodsVo.getProducter())//产品线
                .eq(StringUtils.isNotBlank(goodsVo.getMeteriType()),GoodsVo::getMeteriType,goodsVo.getMeteriType())//材质
                .eq(StringUtils.isNotBlank(goodsVo.getCategoryType()),GoodsVo::getCategoryType,goodsVo.getCategoryType())//品类
                .eq(StringUtils.isNotBlank(goodsVo.getPurity()),GoodsVo::getPurity,goodsVo.getPurity())//成色
                .ge(goodsVo.getStartTime() != null,GoodsVo::getTime,goodsVo.getStartTime())//时间
                .le(goodsVo.getEndTime() != null,GoodsVo::getTime,goodsVo.getEndTime())//时间
                .eq(goodsVo.getAid() != null,GoodsVo::getAid,goodsVo.getAid())//操作员
                .eq(goodsVo.getStatus() != null,GoodsVo::getStatus,goodsVo.getStatus());

//        Page<GoodsVo> goodsList = goodsMapper.selectPage(page,lqw);
//        return goodsList;
        List<Goods> bookAndUser = goodsMapper.selectPageCondition(page,lqw);
        Page<Goods> pp = page.setRecords(bookAndUser);
//        //分页总页数
//        System.out.println(page.getPages());
//        //当前页数量
//        System.out.println(page.getSize());
//        //查询到的记录总条数
//        System.out.println(page.getTotal());
//        //当前页
//        System.out.println(page.getCurrent());
//        //数据
//        System.out.println(page.getRecords());

        return pp;
    }

    @Override
    public Page<Goods> selectPageCondition01(GoodsVo goodsVo){
        goodsVo.setStatus("1");
        // 此处若使用的对象在数据库中没有真实存在的话(即没用相应的mapper.xml)，那么需要手动将缓存存入到 lambda 中去 (lambda 有提供辅助类 TableInfoHelper)
        // 其次 如果使用的是数据库中的表对象，如果字段有 @TableField(exist = false) 注解
        // 那么不能以这个字段作为lambda 的条件，因为有这个注解那么这个列就不会被加载到 lambda 的缓存当总去
        // 添加下面这行就能解决这2个问题
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), GoodsVo.class);

//        //将date转为String
//        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        goodsVo.setStatus("0");
        //当前页，每页大小
        Page<Goods> page = new Page<>(goodsVo.getCurrentPage(), goodsVo.getSize());
        LambdaQueryWrapper<GoodsVo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(goodsVo.getGoodsName()),GoodsVo::getGoodsName,goodsVo.getGoodsName())
                .eq(StringUtils.isNotBlank(goodsVo.getGoodsNumber()),GoodsVo::getGoodsNumber,goodsVo.getGoodsNumber())//商品编号
                .eq(StringUtils.isNotBlank(goodsVo.getArea()),GoodsVo::getArea,goodsVo.getArea())//区域
                .eq(StringUtils.isNotBlank(goodsVo.getProducter()),GoodsVo::getProducter,goodsVo.getProducter())//产品线
                .eq(StringUtils.isNotBlank(goodsVo.getMeteriType()),GoodsVo::getMeteriType,goodsVo.getMeteriType())//材质
                .eq(StringUtils.isNotBlank(goodsVo.getCategoryType()),GoodsVo::getCategoryType,goodsVo.getCategoryType())//品类
                .eq(StringUtils.isNotBlank(goodsVo.getPurity()),GoodsVo::getPurity,goodsVo.getPurity())//成色
                .ge(goodsVo.getStartTime() != null,GoodsVo::getTime,goodsVo.getStartTime())//时间
                .le(goodsVo.getEndTime() != null,GoodsVo::getTime,goodsVo.getEndTime())//时间
                .eq(goodsVo.getAid() != null,GoodsVo::getAid,goodsVo.getAid())//操作员
                .ne(goodsVo.getStatus() != null,GoodsVo::getStatus,goodsVo.getStatus());

//        Page<GoodsVo> goodsList = goodsMapper.selectPage(page,lqw);
//        return goodsList;


        List<Goods> bookAndUser = goodsMapper.selectPageCondition(page,lqw);

        Page<Goods> pp = page.setRecords(bookAndUser);
//        //分页总页数
//        System.out.println(pp.getPages());
//        //当前页数量
//        System.out.println(pp.getSize());
//        //查询到的记录总条数
//        System.out.println(pp.getTotal());
//        //当前页
//        System.out.println(pp.getCurrent());
//        //数据
//        System.out.println(pp.getRecords());

        //查询编号输入的name，乌龙
        //IPage<GoodsVo> bookAndUser = goodsMapper.selectPageCondition01(page,goodsVo);

        return pp;
    }

    @Override
    public Boolean addGoods(Goods goods,Integer aid) {
        goods.setTime(new Date());
        goods.setStatus("3");//3为未定价
        goods.setUnitPrice(new BigDecimal(0));
        goods.setAid(aid);
        goods.setPicture("images_erp/" + goods.getPicture());
        String code = "";
        if(goods.getCategoryType().equals("项链")){
            code = "xl-" + String.valueOf(System.currentTimeMillis());
        } else if(goods.getCategoryType().equals("戒指")){
            code = "jz-" + String.valueOf(System.currentTimeMillis());
        } else if(goods.getCategoryType().equals("手链")){
            code = "sl-" + String.valueOf(System.currentTimeMillis());
        }else if(goods.getCategoryType().equals("耳环")) {
            code = "eh-" + String.valueOf(System.currentTimeMillis());
        }else if(goods.getCategoryType().equals("手镯")) {
            code = "sz-" + String.valueOf(System.currentTimeMillis());
        }else{
            code = "qt-" + String.valueOf(System.currentTimeMillis());
        }

        goods.setGoodsNumber(code);
        if(goodsMapper.insert(goods) > 0){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public List<Goods> findByListNumber(List<AddGoodsVo> addGoodsList) {

        return goodsMapper.findByListNumber(addGoodsList);
    }

    @Override
    public Boolean updateGoods(UpdateGoodsVo updateGoodsVo) {
        Goods goods = new Goods();
        goods.setId(updateGoodsVo.getId());
        goods.setGoodsName(updateGoodsVo.getGoodsName());
        goods.setMeteriType(updateGoodsVo.getMeteriType());
        goods.setCategoryType(updateGoodsVo.getCategoryType());
        goods.setPurity(updateGoodsVo.getPurity());
        goods.setWeight(updateGoodsVo.getWeight());
        goods.setPicture(updateGoodsVo.getPicture());
        String code = "";
        if(goods.getCategoryType().equals("项链")){
            code = "xl-" + updateGoodsVo.getGoodsNumber().substring(3);
        } else if(goods.getCategoryType().equals("戒指")){
            code = "jz-" + updateGoodsVo.getGoodsNumber().substring(3);
        } else if(goods.getCategoryType().equals("手链")){
            code = "sl-" + updateGoodsVo.getGoodsNumber().substring(3);
        } else if(goods.getCategoryType().equals("耳环")) {
            code = "eh-" + updateGoodsVo.getGoodsNumber().substring(3);
        } else if(goods.getCategoryType().equals("手镯")) {
            code = "sz-" + updateGoodsVo.getGoodsNumber().substring(3);
        }else{
            code = "qt-" + updateGoodsVo.getGoodsNumber().substring(3);
        }
        goods.setGoodsNumber(code);
        if(goodsMapper.updateById(goods) > 0){
            return true;
        } else{
            return false;
        }

    }

    @Override
    public List<Goods> findAllGoodsNumber() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.select("goods_number");
        return goodsMapper.selectList(wrapper);
    }




}
